//
//  ChargeRecordViewController.m
//  CS Inventory Manager
//
//  Created by Billy Zoellers on 3/21/13.
//  Copyright (c) 2013 Transylvania University. All rights reserved.
//

#import "ChargeRecordViewController.h"

@interface ChargeRecordViewController ()

@end

@implementation ChargeRecordViewController
@synthesize theRecord,delegate;

- (IBAction)doneButton:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    // Return the number of sections.
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger numRows = 0;
    
    switch (section) {
        case 0:
            numRows = 3;
            break;
        case 1:
            numRows = 2;
            break;
        case 2:
            numRows = 3;
            break;
    }
    
    return numRows;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *title = nil;
    
    // depends on section
    switch (section) {
            // asset info
        case 0:
            title = @"Item Information";
            break;
        case 1:
            title = @"Patron Information";
            break;
        case 2:
            title = @"Charge Information";
            break;
    }
    
    return title;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"detailCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // might need to format some dates
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MM/dd/yyyy"];
    
    switch (indexPath.section) {
        case 0:
            // item info - depends on the row
            switch (indexPath.row) {
                case 0:
                    cell.textLabel.text = @"Type";
                    cell.detailTextLabel.text = [theRecord itemType];
                    break;
                case 1:
                    cell.textLabel.text = @"Model";
                    cell.detailTextLabel.text = [theRecord itemModel];
                    break;
                case 2:
                    cell.textLabel.text = @"Asset No.";
                    cell.detailTextLabel.text = [theRecord assetNo];
                    break;
            }
            break;
        case 1:
            // patron info - only one row
            switch (indexPath.row) {
                case 0:
                    cell.textLabel.text = @"Name";
                    cell.detailTextLabel.text = [theRecord patronName];
                    break;
                case 1:
                    cell.textLabel.text = @"Email";
                    cell.detailTextLabel.text = [theRecord patronEmail];
                    break;
            }
            break;
        case 2:
            // checkout info - depends on row
            switch (indexPath.row) {
                case 0:
                    cell.textLabel.text = @"Date";
                    cell.detailTextLabel.text = [dateFormat stringFromDate:[theRecord chargeDate]];
                    break;
                case 1:
                    cell.textLabel.text = @"Amount";
                    cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@",[NSNumber numberWithDouble:abs([[theRecord chargeAmt] doubleValue])]];
                    
                    // red if it's a charge
                    if ([[theRecord chargeAmt] intValue] > 0) {
                        cell.detailTextLabel.textColor = [UIColor redColor];
                    }
                    
                    break;
                case 2:
                    cell.textLabel.text = @"Notes";
                    cell.detailTextLabel.text = [theRecord chargeNotes];
                    break;
            }
            break;
    }
    
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end
